/**
 * Created by fanfz_000 on 2015/4/7 0007.
 */
$(document).ready(function () {
    var page = 0;
    var page_num;
    var limit = 5;
    $.ajax({
        type: "POST",
        url: APP + "/smileys/index",
        async: true,
        success: function (data) {
            $("#smiley_table").append(data);
        },
        error: function () {
            $("#smiley_table").append("");
        }
    });
    function show_reply_list(page, limit) {
        $.ajax({
            type: "POST",
            url: APP + "/reply/reply_page",
            data: {
                "page": page,
                "limit": limit
            },
            async: true,
            dataType: "json",
            success: function (html) {
                if (html['total'] > 0) {
                    $("#reply_list").empty();
                    $.each(html["data"], function (index, item) {
                        $("#reply_list").append("<div class='recent-single-post'><strong class='text-center text-primary'>" + item["reply_text"] + "</strong><div class='date text-info'>" + item["reply_time"] + "</div></div>");
                    });
                    $("#show_page_num").show();
                    page_num = Math.ceil(html['total'] / limit);
                    $("#this_page").html(page + 1);
                    $("#page_num").html(page_num);
                    if (page == 0) {
                        $("#page_pre").hide();
                    }
                    else {
                        $("#page_pre").show();
                    }
                    if ((page + 1) == page_num) {
                        $("#page_next").hide();
                    }
                    else {
                        $("#page_next").show();
                    }
                }
                else {
                    $("#page_next").hide();
                    $("#page_pre").hide();
                    $("#show_page_num").hide();
                    $("#reply_list").append("<div class='recent-single-post'><strong class='text-center'>暂无记录</strong><div class='date'></div></div>");
                }
            },
            error: function () {
                $("#reply_list").append("<div class='recent-single-post'><strong class='text-center'>网络异常</strong><div class='date'> </div></div>");
            }
        });
    }
    show_reply_list(page, limit);
    $("#page_pre").click(function () {
        page = page - 1;
        show_reply_list(page, limit);
    });
    $("#page_next").click(function () {
        page = page + 1;
        show_reply_list(page, limit);
    });

    $("#editor_reset").click(function () {
        $('#editor').cleanHtml()
    });
    $("#editor").click(function () {
        $('#editor').cleanHtml()
    });
$("#editor_sumbit").click(function () {
    var editor_text = $('#editor').html();
    if (editor_text.length == 0) {
        $("#editor_warning").attr("class", "text-warning");
        $("#editor_warning").empty();
        $("#editor_warning").append("留言不能为空");
    }
    else {
        $.ajax({
            type: "POST",
            url: APP + "/reply/reply_create",
            data: {
                "reply_text": editor_text
            },
            async: true,
            dataType: "json",
            success: function (html) {
                if (html > 0) {
                    $("#editor_warning").attr("class", "text-success");
                    $("#editor_warning").empty();
                    $("#editor_warning").append("留言成功");
                    page = 0;
                    limit = 5;
                    show_reply_list(page, limit);
                }
                else {
                    if (html == -1) {
                        $("#editor_warning").attr("class", "text-warning");
                        $("#editor_warning").empty();
                        $("#editor_warning").append("数据异常");
                    }
                    else if (html == -2) {
                        $("#editor_warning").attr("class", "text-warning");
                        $("#editor_warning").empty();
                        $("#editor_warning").append("留言不能为空");
                    }
                }
            },
            error: function () {
                $("#editor_warning").attr("class", "text-danger");
                $("#editor_warning").empty();
                $("#editor_warning").append("网络异常");
            }
        });
    }
});

});